/*
 * random data generator
 */
Ext.require(['Ext.data.*']);

Ext.onReady(function() {
	window.getRandom = function(m, n) {
    	m = (!m) ? 0 : m;
    	n = (!n && n !== 0) ? 1000 : n;
    	
    	return Math.floor(Math.random() * (n - m)) + m;
	};
	
    window.genCpuData = function(m, n) {
    	var data = [];
    	data.push({
    		cpuData: getRandom(m, n),
    		memData: getRandom(m, n),
    	});
    	return data;
    };

    genTrafficData = function(num, m, n) {
    	var data = [];
        var i;
        
        for (i = 0; i < num; i++) {
            data.push({
                time: i,
                traffic: 0, //getRandom(m, n),
            });
        }
        return data;
    };
    
    var feedTrafficData = function() {
    	var parent = this;
    	//console.log(this);
    	var col = parent.data;
    	//var count = col.getCount();
    	//console.log("store len: %d\n", count);
    	var str = 'start';
    	//var fieldList = self.getFields();
    	//console.log(fieldList.toString());
    	var timeArray = [];
    	var tmp = [];
    	col.eachKey(function(key, item) {
    		//console.log("[%s:%s]", item.data.time, item.data.traffic);
    		str = Ext.String.format('{0}, {1}', str, parent.dumpItem(item));
    		tmp.push(item.data.traffic);
    		timeArray.push(item.data.time);
    		//console.log(key);
    		//console.log(item);
    		return true;
    	});
		//console.log(str);
		//console.log(tmp.toString());
    	tmp.shift();
    	//console.log("after shift: array len %d", tmp.length);
    	//console.log(tmp.toString());
    	tmp.push(getRandom());
    	//console.log("after push: array len %d", tmp.length);
    	//console.log(tmp.toString());
    	
    	parent.removeAll(true);
    	
    	str = 'start';
    	var newData = [];
    	Ext.Array.each(timeArray, function(val, index) {
    		str = Ext.String.format('{0}, {1}:{2}', str, index, val);
    		newData.push({
    			time: val,
    			traffic: tmp[index],
    		});
    		return true;
    	});
    	parent.add(newData);
    	//console.log(str);
    };
    
    var feedConnData = function() {
    	var parent = this;
    	var col = parent.data;
    	//var count = col.getCount();
    	//console.log("store len: %d\n", count);
    	var str = 'start';
    	var timeArray = [];
    	var curConn = [];
    	var newConn = [];
    	col.eachKey(function(key, item) {
    		//console.log("[%s:%s]", item.data.time, item.data.traffic);
    		str = Ext.String.format('{0}, {1}', str, parent.dumpItem(item));
    		curConn.push(item.data.cur_conn);
    		newConn.push(item.data.new_conn);
    		timeArray.push(item.data.time);
    		//console.log(key);
    		//console.log(item);
    		return true;
    	});
		//console.log(str);
		//console.log(tmp.toString());
    	curConn.shift();
    	newConn.shift();
    	//console.log("after shift: array len %d", tmp.length);
    	//console.log(tmp.toString());
    	curConn.push(getRandom());
    	newConn.push(getRandom());
    	//console.log("after push: array len %d", tmp.length);
    	//console.log(tmp.toString());
    	
    	parent.removeAll(true);
    	
    	str = 'start';
    	var newData = [];
    	Ext.Array.each(timeArray, function(val, index) {
    		str = Ext.String.format('{0}, {1}:{2}', str, index, val);
    		newData.push({
    			time: val,
    			cur_conn: curConn[index],
    			new_conn: newConn[index],
    		});
    		return true;
    	});
    	parent.add(newData);
    	//console.log(str);
    };

    genConnData = function(num, m, n) {
    	var data = [];
        var i;
        
        for (i = 0; i < num; i++) {
            data.push({
                time: i,
                cur_conn: 0, //getRandom(m, n),
                new_conn: 0, //getRandom(m, n/2),
            });
        }
        return data;
    };

    window.cpuStore = Ext.create('Ext.data.JsonStore', {
    	fields: ['cpuData', 'memData'],
    	data: genCpuData(1, 100),
    });

    window.trafficStore = Ext.create('Ext.data.JsonStore', {
    	fields: ['time', 'traffic'],
    	data: genTrafficData(30, 0, 1000),
    	dumpItem: function(item) {
        	//console.log(fields.toString());
    		var str = Ext.String.format('{0}:{1}', item.data.time, item.data.traffic);
    		return str;
    	},
    	feedData: feedTrafficData,
    });
    
    trafficStore.getFields = function() {
    	//console.log(this.me.fields);
		return ['time', 'traffic'];
	};

    window.connStore = Ext.create('Ext.data.JsonStore', {
    	fields: ['time', 'cur_conn', 'new_conn'],
    	data: genConnData(30, 0, 1000),
    	dumpItem: function(item) {
    		var str = Ext.String.format('{0}:{1}-{2}', item.data.time, 
    				item.data.cur_conn, item.data.new_conn);
    		return str;
    	},
    	feedData: feedConnData,
    });

    window.vsStore = Ext.create('Ext.data.JsonStore', {
    	fields: ['vs'],
    	data: [{
    		vs: 'vserver1'
    	}, {
    		vs: 'vserver2'
    	}, {
    		vs: 'vserver3'
    	}]
    });
});
